package com.flying.invest.base.service;

import java.util.Date;
import java.util.List;

import com.flying.common.annotation.DaoCreate;
import com.flying.common.annotation.DaoQuery;
import com.flying.common.annotation.DaoRemove;
import com.flying.common.annotation.DaoUpdate;
import com.flying.common.annotation.Transaction;
import com.flying.common.util.Codes;
import com.flying.framework.annotation.MethodInfo;
import com.flying.framework.annotation.Param;
import com.flying.framework.annotation.Service;
import com.flying.framework.model.ActionResult;
import com.flying.framework.model.ModelFactory;
import com.flying.framework.model.QueryResult;
import com.flying.framework.service.AbstractService;
import com.flying.invest.base.model.Currency;
import com.flying.invest.base.spider.CurrencyRate;
import com.flying.invest.base.spider.CurrencyRateSpider;

@Service(value="CurrencyService", desc="")
public class CurrencyService extends AbstractService{
	private CurrencyHistoryService chs;
	private CurrencyRateSpider spider;
	
	public void setCurrencyHistoryService(CurrencyHistoryService s) {
		this.chs = s;
	}
	public void setCurrencyRateSpider(CurrencyRateSpider s) {
		this.spider = s;
	}
        
	@MethodInfo("新增")
	@DaoCreate(entity=Currency.ENTITY_NAME)
	public Currency create(
			@Param(value=Currency.CODE, required=true,	desc="币种代码") String code,
			@Param(value=Currency.NAME, required=true,	desc="币种名称") String name,
			@Param(value=Currency.RATE, required=true,	desc="汇率") Double rate,
			@Param(value=Currency.CREATE_TIME, required=false,	desc="创建时间") Date create_time
		) throws Exception {
		return ModelFactory.create(Currency.class, 
				Currency.CREATE_TIME, new Date());
	}

	@MethodInfo("修改")
	@Transaction
	@DaoUpdate(entity=Currency.ENTITY_NAME)
	public ActionResult update(
			@Param(value=Currency.CURRENCY_ID, required=true,	desc="币种ID") Long currency_id,
			@Param(value=Currency.CODE, required=true,	desc="币种代码") String code,
			@Param(value=Currency.NAME, required=true,	desc="币种名称") String name,
			@Param(value=Currency.RATE, required=true,	desc="汇率") Double rate,
			@Param(value=Currency.UPDATE_TIME, required=false,	desc="更新时间") Date update_time
		) {
		this.chs.create(currency_id, code, rate, new Date(), new Date());
		return ModelFactory.create(ActionResult.class, 
				Currency.UPDATE_TIME, new Date());
	}

	@MethodInfo("查询")
	public ActionResult fetchCurrencyRateById(
			@Param(value=Currency.CURRENCY_ID, required=true,	desc="币种ID") Long currency_id
		) throws Exception {
		Currency c = this.findById(currency_id);
		CurrencyRate r = this.spider.fetch(c.getCode());
		return this.update(currency_id, c.getCode(), c.getName(), r.getRate(), new Date());
	}

	@MethodInfo("查询")
	public ActionResult fetchAllCurrencyRate() {
		List<Currency> list = this.findAll(0, Integer.MAX_VALUE).getRows();
		list.stream().forEach(c -> {
			try {
				CurrencyRate r = this.spider.fetch(c.getCode());
				this.update(c.getCurrencyId(), c.getCode(), c.getName(), r.getRate(), new Date());
			} catch (Exception e) {
				e.printStackTrace();
			}
		});
		return ModelFactory.create(ActionResult.class, Codes.CODE, Codes.SUCCESS);
	}

	@MethodInfo("删除")
	@DaoRemove(entity=Currency.ENTITY_NAME)
	public ActionResult remove(
			@Param(value=Currency.CURRENCY_ID, required=true,	desc="币种ID") Long currency_id
		) throws Exception {
		return null;
	}

	@MethodInfo("搜索")
	@DaoQuery(entity=Currency.ENTITY_NAME, pageable=true, modelClass=Currency.class)
	public QueryResult<Currency> findAll(
			@Param(value=Codes.PAGE_NUM, 	required=false, desc="分页页号，默认0") int page,
			@Param(value=Codes.PAGE_SIZE, 	required=false, desc="分页大小，默认10") int rows) {
		return null;
	}

	@MethodInfo("查询")
	@DaoQuery(entity=Currency.ENTITY_NAME, single=true, throwsNotFoundException=true )
	public Currency findById(
			@Param(value=Currency.CURRENCY_ID, required=true,	desc="币种ID") Long currency_id
		) throws Exception {
		return null;
	}

	@MethodInfo("查询")
	@DaoQuery(entity=Currency.ENTITY_NAME, single=true, throwsNotFoundException=true )
	public Currency findByCode(
			@Param(value=Currency.CODE, required=true,	desc="币种代码") String code
		) {
		return null;
	}

}